package profiling;

import com.google.inject.AbstractModule;

import de.mlp_distributed.mlp.core.SparseAutoencoder;
import de.mlp_distributed.mlp.math.Factory;
import de.mlp_distributed.mlp.math.MahoutMatrixFactory;
import de.mlp_distributed.mlp.math.MahoutVectorFactory;
import de.mlp_distributed.mlp.math.MatrixFactory;
import de.mlp_distributed.mlp.math.VectorFactory;
import de.mlp_distributed.mlp.math.mahout.RandomUtils;
import de.mlp_distributed.mlp.math.mahout.Vector;

public class SimpleAutoencoder {

	private static final int dim = 500;
	private static Factory factory;
	private static VectorFactory vectorFactory;

	static {
		SimpleAutoencoder.factory = Factory.getInstance(new AbstractModule() {

			@Override
			protected void configure() {
				this.bind(VectorFactory.class).to(MahoutVectorFactory.class);
				this.bind(MatrixFactory.class).to(MahoutMatrixFactory.class);

				// this.bind(VectorFactory.class).to(JBlasVectorFactory.class);
				// this.bind(MatrixFactory.class).to(JBlasMatrixFactory.class);
			}
		});
		SimpleAutoencoder.vectorFactory = SimpleAutoencoder.factory.getVectorFactory();
	}

	public static void main(final String[] args) throws Exception {
		final SparseAutoencoder auto = new SparseAutoencoder(SimpleAutoencoder.dim);
		int iterations = 0;
		long sum = 0;
		auto.initWeightsRandomly(RandomUtils.getRandom());
		for (int i = 0; i < 1000000; i++) {

			final long start = System.currentTimeMillis();
			auto.getDerivativeOfTheCostWithoutRegularization(SimpleAutoencoder.getIdentityVector(), SimpleAutoencoder.getIdentityVector());
			final long end = System.currentTimeMillis();
			//
			// if (i < 10) {
			// continue;
			// }
			iterations++;
			sum += (end - start);
			System.out.println((end - start) + "ms");
			// final Vector result =
			// auto.classifyFull(SimpleAutoencoder.getIdentityVector());
			// final double cost = auto.getCost(result,
			// SimpleAutoencoder.getIdentityVector());
			// auto.close();
			// System.out.println(cost);
		}
		System.out.println("Average: " + (sum / iterations));
	}

	static Vector getIdentityVector() {
		final Vector vector = SimpleAutoencoder.vectorFactory.construct(SimpleAutoencoder.dim);
		vector.setQuick(0, 1);
		vector.setQuick(1, 1);
		vector.setQuick(2, 1);
		return vector;
	}
}
